<template>
	<fui-form>
		<custom-form-item-card>
			<fui-form-item label="转交给" label-width="auto" arrow :bottom-border="false" asterisk>
				<template #right>
					<fui-text type="primary" :text="labelWho" :size="32" @click="choseWho"></fui-text>
				</template>
			</fui-form-item>
			<fui-form-item label="转交原因" label-width="auto" asterisk>
				<template #vertical>
					<fui-radio-group v-model="formData.reason">
						<fui-label v-for="(item,index) in reasonList" :key="index">
							<fui-list-cell>
								<view class="fui-align__center">
									<fui-radio :checked="item.checked" :value="item.value" color="#0052D9"
										borderColor="#0052D9">
									</fui-radio>
									<fui-text style="margin-left: 18rpx;" class="fui-text" :text="item.label"
										size="32"></fui-text>
								</view>
							</fui-list-cell>
						</fui-label>
					</fui-radio-group>
				</template>

			</fui-form-item>
			<fui-form-item label="说明" label-width="auto" :bottom-border="false">
				<template #vertical>
					<fui-textarea v-model="formData.remark" :borderTop="false" placeholder="请输入"></fui-textarea>
				</template>
			</fui-form-item>

		</custom-form-item-card>

	</fui-form>
</template>

<script>
	import {
		defineComponent,
	} from "vue"
	import {
		useHandleWorkOrder_Transfer
	} from '@/store/modules/handleWorkOrder_Transfer.js'
	import {
		customValue,
		DeptSelectChoseType,
		getDicValue,
		toDeptSelectPage,
		toTeamUserSelectPage
	} from "../../../../utils/util"
	import {
		FormTeamUserStoreKey
	} from "../../../../store/sotreKey"
	import {
		useDeptStore
	} from "../../../../store/modules/dept"
	import {
		mapState,
		mapWritableState
	} from 'pinia'
	import {
		useDictStore
	} from "../../../../store/modules/dic"
	import {
		useSyStemStore
	} from "../../../../store/modules/system"
	import {
		DIC_KEY
	} from "../../../../config/dicKey-config"
	import {
		WorkOrderMyIndexApi
	} from "../../../../api/workOrder"
	import form from '@/components/firstui/fui-validator/fui-validator'
	import _ from 'lodash'
	import {
		useTeamUserStore
	} from "../../../../store/modules/teamUser"
	export default defineComponent({
		data() {
			return {

			}
		},
		computed: {
			...mapWritableState(useHandleWorkOrder_Transfer, ['formData']),
			...mapState(useTeamUserStore, ['teamUserStoreKey']),
			...mapState(useSyStemStore, ['dicList']),
			reasonList() {
				return getDicValue(this.dicList, DIC_KEY.obs_trans_reason)
			},

			labelWho() {
				const {
					value
				} = this.teamUserStoreKey[FormTeamUserStoreKey.MyWorkOrderHandleTransferWho.key]
				const type = {
					"team": 2,
					"user": 1
				}
				if (value.length) {

					const v = JSON.parse(value[0])
					this.formData.sendObjType = type[v.key]
					this.formData.sendObjId = Number(v.value)
					this.formData.sendTeam = type[v.key] === 1 ? v.value : ''
					return v.label
				}
				return '请选择'
			},
		},
		emits: ['disabled'],
		methods: {
			choseWho() {
				toTeamUserSelectPage(FormTeamUserStoreKey.MyWorkOrderHandleTransferWho.key, false)
			},
			async submit(orderNum, cb) {
				const rules = [{
					name: "sendObjType",
					rule: ["required"],
					msg: ["请选择转交"]

				}, {
					name: "reason",
					rule: ["required"],
					msg: ["请选择转交原因"]
				}];
				try {
					this.formData.orderNum = orderNum
					const the = this
					let {
						isPassed,
						errorMsg
					} = form.validator(this.formData, rules)
					if (isPassed) {
						this.$emit('disabled', true)
						await WorkOrderMyIndexApi.transferOrder(this.formData)
						this.fui.toast('转交完成')
						_.delay(() => {
							the.formData = {
								orderNum: '',
								//  转单原因
								reason: '',
								//  转单说明
								remark: '',
								//  派单对象标识
								sendObjId: '',
								//  派单对象类型 1 用户 2小组 3部门
								sendObjType: '',
								//  派单所属小组
								sendTeam: '',
							}

							cb && cb()
						}, 1000)

					} else {
						this.$emit('disabled', false)
						this.fui.toast(errorMsg)
					}
				} catch (err) {
					this.$emit('disabled', false)
					console.error(err);
					this.fui.toast(err)

				}
			}
		},
	})
</script>

<style>
</style>